package modellayer;

//import
import java.util.ArrayList;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

public class OrdreCont 
{
    private static OrdreCont instance;
    private ArrayList<Ordre> ordrer;
    private OrdreFileHandler fileHandler;

    /**
     * OrdreCont Constructor
     *
     */
    public OrdreCont()
    {
        ordrer = new ArrayList<Ordre>();
        fileHandler = new OrdreFileHandler("ordreDate");
    }

    /**
     * Singleton Method getInstance
     *
     * Creates a OrdreCont if one does not exist 
     * @return The return value
     */
    public static OrdreCont getInstance()
    {
        if(instance == null)
        {
            instance = new OrdreCont();
        }
        return instance;
    }

    /**
     * Method addOrdre
     *
     * adds an order to the arraylist of orders.
     * 
     * @param o An order
     * @return True if succesful. False if not.
     */
    public boolean addOrdre(Ordre o)
    {
        boolean succes = false;
        ordrer.add(o);
        if(getOrdre(o.getOrdreNr()) != null)
        {
            succes = true;
        }
        return succes;
    }

    /**
     * Method deleteOrdre
     *
     * Removes an order with the given ordernumber.
     * 
     * @param OrdreNr Ordernumber
     * @return False if the orderlist is empty. True if the order is removed.
     */
    public boolean deleteOrdre(int OrdreNr)
    {
        boolean succes = false;
        Ordre removeOrdre =  getOrdre(OrdreNr);
        ordrer.remove(removeOrdre);
        if(ordrer.isEmpty())
        {
            return succes;
        }
        else if(!ordrer.contains(removeOrdre))
        {
            succes = true;
        }
        return succes;
    }

    /**
     * Method getOrdrer
     *
     * @return A list with orders
     */
    public ArrayList<Ordre> getOrdrer()
    {
        return ordrer;
    }
        
    /**
     * Method getOrdre
     *
     * Searches for an order with the given ordernumber
     * 
     * @param OrdreNr Ordernumber
     * @return An order of the type Ordre
     */
    public Ordre getOrdre(int OrdreNr)
    {
        Ordre returnOrdre = null;
        int i = 0;
        boolean found = false;
        while(!found && i < ordrer.size())
        {
            if(ordrer.get(i).getOrdreNr() == OrdreNr)
            {
                returnOrdre = ordrer.get(i); 
                found = true;
            }
            else
            {
                i++;
            }
        }
        return returnOrdre;
    }

    /**
     * Method updateOrdreStatus
     *
     * Updates all given parameters of an order
     * 
     * @param OrdreNr ordernumber.
     * @param status status
     * @return True if the update succeded. False if not.
     */
    public boolean updateOrdreStatus(int OrdreNr, String status)
    {
        boolean succes = false;
        Ordre updateOrdre = getOrdre(OrdreNr);
        updateOrdre.setStatus(status);
        if(updateOrdre.getStatus().equals(status))
        {
            succes = true;
        }
        return succes;
    }
    
    /**
     * Method ordrerToString
     * 
     * Returns a string with information about an order 
     *
     * @return returnString
     */
    public String ordrerToString()
    {
        String returnString = "";
        for(int i = 0; i < ordrer.size();i++)
        {
            returnString += ordrer.get(i).toString();
        }
        return returnString;
    }
    
    /**
     * Method saveToFile
     * 
     * Saves to the vareFileHandler
     */
    public void saveToFile() throws IOException
    {
        fileHandler.saveToFileOrdre(ordrer);
    }

    /**
     * Method loadFromFile
     *
     * Loads from the vareFileHandler
     */
    public void loadFromFile() throws IOException, ClassNotFoundException
    {
        ordrer = fileHandler.readFromFileOrdre();
    }

}
